/* -*- coding: utf-8; tab-width: 4 -*-
vim: ts=4 noet ai */

/**
	Hide Preferences for Firefox

	Copyright © 2016  Desktopd Project

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as
	published by the Free Software Foundation, either version 3 of the
	License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

	@license GPL-3.0+
	@file
*/

'use strict';

// Preference changes only take effect on new windows
const prefs = require ('./prefs');
const {getResourceURI} = require ('./resource');
const contentScript = require ('./content-script');
const windowMod = require ('./window-mod');

contentScript.register ('about:addons', 'content/addons.js', 'addons');
contentScript.register ('about:preferences', 'content/preferences.js', 'preferences');

const removeStyleSheets = window => {
	const d = window.document;
	const base = getResourceURI ('chrome/');
	
	for (let node of d.childNodes) {
		if (d.PROCESSING_INSTRUCTION_NODE !== node.nodeType) continue;
		if ('xml-stylesheet' !== node.nodeName) continue;
		if (!node.sheet) continue;
		
		const href = node.sheet.href;
		if (base !== href.substr (0, base.length)) continue;
		
		node.remove ();
	}
};

const refreshChrome = window => {
	const o = prefs.getBranch ('chrome');
	const d = window.document;
	
	removeStyleSheets (window);
	
	// DIRTY: There is no other way
	const addStylesheet = href => {
		const stylesheet = d.createProcessingInstruction ('xml-stylesheet'
			,`type="text/css" href="${href}"`);
		d.insertBefore (stylesheet, d.documentElement);
	};
	
	if (!o['hamburgerMenu']) {
		addStylesheet (getResourceURI ('chrome/panelUI.css'));
	}
	if (!o['hamburgerMenu.signInToSync']) {
		addStylesheet (getResourceURI ('chrome/panelUI-fxa.css'));
	}
	if (!o['hamburgerMenu.help']) {
		addStylesheet (getResourceURI ('chrome/panelUI-help.css'));
	}
	if (!o['hamburgerMenu.help.remote']) {
		addStylesheet (getResourceURI ('chrome/panelUI-help-remote.css'));
	}
	if (!o['hamburgerMenu.help.restartDisabled']) {
		addStylesheet (getResourceURI ('chrome/panelUI-help-safemode.css'));
	}
	if (!o['tab.icon']) {
		addStylesheet (getResourceURI ('chrome/tab-icon.css'));
	}
	if (!o['urlbar.reloadButton']) {
		addStylesheet (getResourceURI ('chrome/reload-button.css'));
	}
	if (!o['urlbar.placeholder']) {
		// This element may not exist
		const urlbar = d.getElementById ('urlbar');
		urlbar && (urlbar.placeholder = '');
	}
};

// Modify chrome
windowMod.listen (refreshChrome);
prefs.listen ('refreshChrome', () => windowMod.getWindows ().forEach (refreshChrome));

exports.onUnload = aReason => 'shutdown' !== aReason
	&& windowMod.getWindows ().forEach (removeStyleSheets);

